package com.nuoniu.sibanyun.mapper.erp;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.entity.dto.ErpAccountingSearchDto;
import com.nuoniu.sibanyun.entity.erp.ErpAccounting;
import com.nuoniu.sibanyun.entity.vo.ErpAccountingVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * 会计科目表 Mapper 接口
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-25
 */
@Mapper
@Component
public interface ErpAccountingMapper extends BaseMapper<ErpAccounting> {
//    @Select("SELECT json_text FROM json_test WHERE id = 1 ")
//    String getJson();
//
//    @Update("UPDATE erp_accinformation SET Description = #{Description} WHERE `Name` = #{Name}")
//    boolean updateAccinformation(String Description,String Name );


    Page<ErpAccountingVo> page(Page page, @Param("dto") ErpAccountingSearchDto erpAccountingSearchDto,@Param("companyId") Integer companyId,Integer accountId);

    @Select("SELECT ea.*,eat.`name` as typeName FROM fab_accounting ea INNER JOIN fab_accounting_type eat ON eat.id = ea.accounting_type_id  WHERE ea.company_id = #{companyId} AND ea.account_id = #{accountId} AND ea.parent_id = #{parentId}")
    List<ErpAccountingVo> listErpAccountingByParentId(Integer parentId,Integer companyId,Integer accountId);

    @Select("SELECT ea.*,eat.`name` as typeName FROM fab_accounting ea INNER JOIN fab_accounting_type eat ON eat.id = ea.accounting_type_id  WHERE ea.company_id =  1 AND FIND_IN_SET(#{industryId},ea.industry_ids)")
    List<ErpAccountingVo> listDefErpAccounting(Integer industryId);

    @Select("SELECT ea.*,eat.`name` as typeName FROM fab_accounting ea INNER JOIN fab_accounting_type eat ON eat.id = ea.accounting_type_id  WHERE ea.company_id = #{companyId} AND ea.account_id = #{accountId} AND ea.accounting_type_id = #{typeId}")
    List<ErpAccountingVo> listFirstLevelErpAccountingByTypeId(Integer companyId,Integer accountId,Integer typeId);

    @Select("SELECT ea.*,eat.`name` as typeName FROM fab_accounting ea INNER JOIN fab_accounting_type eat ON eat.id = ea.accounting_type_id  WHERE ea.company_id = #{companyId} AND ea.account_id = #{accountId}")
    List<ErpAccountingVo> listCurrentAll(Integer companyId,Integer accountId);

    @Select("SELECT * FROM fab_accounting WHERE company_id = #{companyId} AND account_id = #{accountId} ORDER BY id DESC LIMIT 1")
    ErpAccounting getLastId(Integer companyId,Integer accountId);
    @Select("SELECT * FROM fab_accounting WHERE company_id = #{companyId} AND account_id = #{accountId} ORDER BY id ASC LIMIT 1")
    ErpAccounting getFirstId(Integer companyId,Integer accountId);
    @Select("SELECT * FROM fab_accounting WHERE company_id = #{companyId} AND account_id = #{accountId} AND id < #{currentId} ORDER BY id DESC LIMIT 1")
    ErpAccounting getPreId(Integer companyId,Integer accountId,Long currentId);
    @Select("SELECT * FROM fab_accounting WHERE company_id = #{companyId} AND account_id = #{accountId} AND id > #{currentId} ORDER BY id ASC LIMIT 1")
    ErpAccounting getNextId(Integer companyId,Integer accountId,Long currentId);

    @Select("select count(1) from fab_accounting where parent_id=#{id}")
    Integer getByParentId(Long id);

    /**
     * 初始化会计凭证
     * @author 小啊刚
     * @date 2021-04-20
     */
    @Insert("insert into fab_accounting (company_id, account_id, code, name, tree_names, accounting_type_id, accounting_type_name, dc_direction, grade_rule_level, grade_rule_str) " +
            " select #{companyId}, #{accountId}, code, name, tree_names, accounting_type_id, accounting_type_name, dc_direction, grade_rule_level, grade_rule_str from fab_accounting where account_id=0")
    void initData(Integer companyId, Integer accountId);

    @Select("select count(1) from fab_accounting where default_currency_id=#{id}")
    Integer getByCurrency(Long id);

    @Select("select * from fab_accounting where id in (${parentIds})")
    List<ErpAccounting> getByParentIds(String parentIds);
}
